//
//  StoreSearchToolView.m
//  much
//
//  Created by weijunjie on 2019/1/4.
//  Copyright © 2019年 iOS. All rights reserved.
//

#import "StoreSearchToolView.h"
#import "SearchFilterModel.h"
@interface StoreSearchBtn : UIButton
@property (nonatomic, assign) BOOL isDESC; // 是否降序
@end

@implementation StoreSearchBtn

- (void)layoutSubviews {
    [super layoutSubviews];
    self.titleLabel.x = (self.width - (self.titleLabel.width + self.imageView.width + 4)) * 0.5;
    self.imageView.x = CGRectGetMaxX(self.titleLabel.frame) + 4;
}

@end

@interface StoreSearchToolView()
/** 综合 */
@property (nonatomic, strong) UIButton *comprehensiveBtn;
/** 销量 */
@property (nonatomic, strong) StoreSearchBtn *salesBtn;
/** 新品 */
@property (nonatomic, strong) UIButton *newsBtn;
/** 价格 */
@property (nonatomic, strong) UIButton *priceBtn;
/** 当前选中 */
@property (nonatomic, strong) UIButton *seletedBtn;
@end
@implementation StoreSearchToolView

- (instancetype)init
{
    self = [super init];
    if (self) {
        CGFloat width = ScreenWidth / 4;
        self.comprehensiveBtn.frame = CGRectMake(0, 0, width, 44);
        self.salesBtn.frame = CGRectMake(width, 0, width, 44);
        self.newsBtn.frame = CGRectMake(width * 2, 0, width, 44);
        self.priceBtn.frame = CGRectMake(width * 3, 0, width, 44);
        self.seletedBtn = self.comprehensiveBtn;
    }
    return self;
}

#pragma mark - 懒加载
- (UIButton *)comprehensiveBtn {
    if (!_comprehensiveBtn) {
        _comprehensiveBtn = [[UIButton alloc] init];
        _comprehensiveBtn.tag = StoreSearchToolComprehensiveType;
        [_comprehensiveBtn setTitle:@"综合" forState:UIControlStateNormal];
        [_comprehensiveBtn setTitleColor:Color175030 forState:UIControlStateNormal];
        _comprehensiveBtn.titleLabel.font = APPFONT(14);
        [self addSubview:_comprehensiveBtn];
        [_comprehensiveBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _comprehensiveBtn;
}

- (StoreSearchBtn *)salesBtn {
    if (!_salesBtn) {
        _salesBtn = [[StoreSearchBtn alloc] init];
        _salesBtn.tag = StoreSearchToolSalesType;
        [_salesBtn setTitle:@"销量" forState:UIControlStateNormal];
        [_salesBtn setTitleColor:Color34 forState:UIControlStateNormal];
        [_salesBtn setImage:[UIImage imageNamed:@"mall_home_price_normal"] forState:UIControlStateNormal];
        _salesBtn.titleLabel.font = APPFONT(14);
        [self addSubview:_salesBtn];
        [_salesBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _salesBtn;
}

- (UIButton *)newsBtn {
    if (!_newsBtn) {
        _newsBtn = [[UIButton alloc] init];
        _newsBtn.tag = StoreSearchToolNewsType;
        [_newsBtn setTitle:@"新品" forState:UIControlStateNormal];
        [_newsBtn setTitleColor:Color34 forState:UIControlStateNormal];
        _newsBtn.titleLabel.font = APPFONT(14);
        [self addSubview:_newsBtn];
        [_newsBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _newsBtn;
}

- (UIButton *)priceBtn {
    if (!_priceBtn) {
        _priceBtn = [[UIButton alloc] init];
        _priceBtn.tag = StoreSearchToolPriceType;
        [_priceBtn setTitle:@"价格" forState:UIControlStateNormal];
        [_priceBtn setTitleColor:Color34 forState:UIControlStateNormal];
        _priceBtn.titleLabel.font = APPFONT(14);
        [self addSubview:_priceBtn];
        [_priceBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _priceBtn;
}
#pragma mark - event
- (void)btnAction:(UIButton *)sender {
    
    // 节流
    self.userInteractionEnabled = NO;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.userInteractionEnabled = YES;
    });
    [self.seletedBtn setTitleColor:Color34 forState:UIControlStateNormal];
    self.seletedBtn = sender;
    [self.seletedBtn setTitleColor:Color175030 forState:UIControlStateNormal];
    if (sender.tag == StoreSearchToolSalesType) {
        self.salesBtn.isDESC = !self.salesBtn.isDESC;
        [self.salesBtn setImage:[UIImage imageNamed:self.salesBtn.isDESC ? @"mall_home_price_up" : @"mall_home_price_low"] forState:UIControlStateNormal];
    } else {
        [self.salesBtn setImage:[UIImage imageNamed:@"mall_home_price_normal"] forState:UIControlStateNormal];
        self.salesBtn.isDESC = NO;
    }
    self.filterModel.orderType = SearchFilterOrderNoneType;
    switch (sender.tag) {
        case StoreSearchToolComprehensiveType:
            self.filterModel.orderFieldType = SearchFilterOrderFieldCompositeType;
            break;
        case StoreSearchToolSalesType:
            self.filterModel.orderFieldType = SearchFilterOrderFieldSaleNumberType;
            self.filterModel.orderType = self.salesBtn.isDESC ? SearchFilterOrderDESCType : SearchFilterOrderASCType;
            break;
        case StoreSearchToolNewsType:
            self.filterModel.orderFieldType = SearchFilterOrderFieldNewestType;
            
            break;
        case StoreSearchToolPriceType:
            self.filterModel.orderFieldType = SearchFilterOrderFieldPriceType;
            
            break;
        default:
            break;
    }
    
    self.filterModel.pageIndex = 0;
    if (self.clickToolBlock) {
        self.clickToolBlock();
    }
    

}


@end
